package com.zzty.zzlp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.zzty.zzlp.dao.UserDao;
import com.zzty.zzlp.entity.User;
import com.zzty.zzlp.service.SelfExamService;
import com.zzty.zzlp.service.SelfExamTransationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/selfgread")
public class SelfExamGreadController {
    @Autowired
    UserDao userDao;
    @Autowired
    SelfExamService selfExamService;
    @Autowired
    SelfExamTransationService selfExamTransationService;

    @RequestMapping(value = "/Call", method = RequestMethod.POST)
    @ResponseBody
    public String scoreSearch(@RequestParam(value = "date") String date, HttpServletRequest request) throws Exception {
        System.out.println("this is call bu date");
        User user = (User) request.getSession().getAttribute("User");
        System.out.println(date);
        Integer clazzId = user.getClazzId();
//         Integer clazzId = 4;
        JSONArray jsonArray = new JSONArray();
        List<Integer> userids = userDao.searchStuIdByclazzId(clazzId);
            for (int i = 0; i < userids.size(); i++) {
                JSONObject json1 = new JSONObject();
                int userId = userids.get(i);
                json1.put("userId", userId);
                json1.put("date", date);
                User stu = userDao.selectUserById(userId);
                json1.put("stuName", stu.getDisname());
                int examId = selfExamService.searchIdByDateAndClass(date, clazzId);
                 ArrayList questions =
                 selfExamService.searchQstId(date,Integer.valueOf(clazzId));
                 ArrayList Questions =
                 selfExamService.searchQstContent(questions);
                 String question = "";
                 for (int k = 0; k < Questions.size(); k++) {
                 question += Questions.get(k) + "\r\n";
                 }
                 json1.put("question", question);
                Integer totalTime = selfExamTransationService.searchTotalTimeById(examId, userId);
                if (totalTime == null) {
                    json1.put("totalTime", "");
                } else {
                    json1.put("totalTime", totalTime);
                }
                String score = selfExamTransationService.searchScore(examId, userId);
                if (score == null) {
                    json1.put("score", "");
                } else {
                    json1.put("score", score);
                }
                System.out.println("score" + score);
                Integer valid = selfExamTransationService.searchIsValid(examId, userId);
                json1.put("valid", valid);
                jsonArray.add(json1);
            }
        return jsonArray.toString();
    }
}
